package com.dragon.app.commons.dto;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import com.dragon.loan.entity.AcApplyEnvironmentInfo;
import com.marvels.common.utils.MarvelsLogUtil;

public class AcApplyEnvironmentinfoDTO implements Serializable{
	private static final long serialVersionUID = 1L;
	public static AcApplyEnvironmentInfo getAcApplyEnvironmentinfo(HttpServletRequest request){
		String idFintechUmUser = request.getHeader("idFintechUmUser");
		String osVersion = request.getHeader("osVersion");
		String timeStamp = request.getHeader("timeStamp");
		String appVersion = request.getHeader("appVersion");
		String terminalNo = request.getHeader("terminalNo");
		String appKey = request.getHeader("appKey");
		String channelName = request.getHeader("channelName");
		String MAC = request.getHeader("MAC");
		String IMEI = request.getHeader("IMEI");
		String brand = request.getHeader("brand");
		String accessToken = request.getHeader("accessToken");
		String isBreak = request.getHeader("isBreak");
		// 新增IP地址

		AcApplyEnvironmentInfo acApplyEnvironmentInfo = new AcApplyEnvironmentInfo();
		acApplyEnvironmentInfo.setIpAddress(getIPByRequest(request));
		acApplyEnvironmentInfo.setAccessToken(accessToken);
		acApplyEnvironmentInfo.setAppKey(appKey);
		acApplyEnvironmentInfo.setAppVersion(appVersion);
		acApplyEnvironmentInfo.setBrand(StringUtils.lowerCase(brand));
		acApplyEnvironmentInfo.getCreatedBy();
		acApplyEnvironmentInfo.setChannelName(StringUtils.lowerCase(channelName));
		acApplyEnvironmentInfo.setIdFintechUmUser(idFintechUmUser);
		acApplyEnvironmentInfo.setImei(IMEI);
		acApplyEnvironmentInfo.setMac(MAC);
		acApplyEnvironmentInfo.setOsVersion(StringUtils.lowerCase(osVersion));
		acApplyEnvironmentInfo.setTerminalNo(terminalNo);
		acApplyEnvironmentInfo.setTimeStamp(timeStamp);
		acApplyEnvironmentInfo.setIsBreak(isBreak);
		return acApplyEnvironmentInfo;
	}
	
	public static AcApplyEnvironmentInfo getAcApplyEnvironmentinfo(HttpServletRequest request, ProductApplyDTO productApplyDTO){
		String idFintechUmUser = request.getHeader("idFintechUmUser");
		String osVersion = request.getHeader("osVersion");
		String timeStamp = request.getHeader("timeStamp");
		String appVersion = request.getHeader("appVersion");
		String terminalNo = request.getHeader("terminalNo");
		String appKey = request.getHeader("appKey");
		String channelName = request.getHeader("channelName");
		String MAC = request.getHeader("MAC");
		String IMEI = request.getHeader("IMEI");
		String brand = request.getHeader("brand");
		String accessToken = request.getHeader("accessToken");
		String isBreak = request.getHeader("isBreak");
		// 新增IP地址

		AcApplyEnvironmentInfo acApplyEnvironmentInfo = new AcApplyEnvironmentInfo();
		acApplyEnvironmentInfo.setIpAddress(getIPByRequest(request));
		acApplyEnvironmentInfo.setAccessToken(accessToken);
		acApplyEnvironmentInfo.setAppKey(appKey);
		acApplyEnvironmentInfo.setAppVersion(appVersion);
		acApplyEnvironmentInfo.setBrand(StringUtils.lowerCase(brand));
		acApplyEnvironmentInfo.getCreatedBy();
		acApplyEnvironmentInfo.setChannelName(StringUtils.lowerCase(channelName));
		acApplyEnvironmentInfo.setIdFintechUmUser(idFintechUmUser);
		acApplyEnvironmentInfo.setImei(IMEI);
		acApplyEnvironmentInfo.setMac(MAC);
		acApplyEnvironmentInfo.setOsVersion(StringUtils.lowerCase(osVersion));
		acApplyEnvironmentInfo.setTerminalNo(terminalNo);
		acApplyEnvironmentInfo.setTimeStamp(timeStamp);
		acApplyEnvironmentInfo.setIsBreak(isBreak);
		acApplyEnvironmentInfo.setDeviceId(productApplyDTO.getDeviceId());
		acApplyEnvironmentInfo.setLon(productApplyDTO.getLon());
		acApplyEnvironmentInfo.setWifiAddress(productApplyDTO.getWifiaddress());
		acApplyEnvironmentInfo.setLat(productApplyDTO.getLat());
		acApplyEnvironmentInfo.setProvince(productApplyDTO.getProvince());
		acApplyEnvironmentInfo.setCity(productApplyDTO.getCity());
		return acApplyEnvironmentInfo;
	}
	
	public static String getIPByRequest(HttpServletRequest request){
		 String ipAddress = null;
	        try {
	            ipAddress = request.getHeader("x-forwarded-for");
	            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
	                ipAddress = request.getHeader("Proxy-Client-IP");
	            }
	            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
	                ipAddress = request.getHeader("WL-Proxy-Client-IP");
	            }
	            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
	                ipAddress = request.getRemoteAddr();
	                if (ipAddress.equals("127.0.0.1")) {
	                    // 根据网卡取本机配置的IP
	                    InetAddress inet = null;
	                    try {
	                        inet = InetAddress.getLocalHost();
	                    } catch (UnknownHostException e) {
	                        MarvelsLogUtil.getInstance().error("=====ERROR=====", e);
	                    }
	                    ipAddress = (inet == null ? null : inet.getHostAddress());
	                }
	            }
	            // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
	            if (ipAddress != null && ipAddress.length() > 15) { // "***.***.***.***".length()
	                // = 15
	                if (ipAddress.indexOf(",") > 0) {
	                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
	                }
	            }
	        } catch (Exception e) {
	            ipAddress="";
	        }
	        // ipAddress = this.getRequest().getRemoteAddr();

	        return ipAddress;
	}
	
}
